package com.tianci.appstore.utils;

import com.coocaa.x.framework.utils.LogUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileTools {

    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        if (tempList != null) {
            File temp = null;
            for (int i = 0; i < tempList.length; i++) {
                if (path.endsWith(File.separator)) {
                    temp = new File(path + tempList[i]);
                } else {
                    temp = new File(path + File.separator + tempList[i]);
                }
                if (temp.isFile()) {
                    temp.delete();
                }
                if (temp.isDirectory()) {
                    LogUtils.d("db", "path :" + (path + "/" + tempList[i]));
                    delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
                    flag = true;
                }
            }
        }
        return flag;
    }

    public static void saveLog(Exception e) {
        String path = "skydir/log.txt";
        LogUtils.d("save", "path:" + path);
        File f = new File(path);
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        StringBuffer sb = new StringBuffer();
        StackTraceElement[] es = e.getStackTrace();
        for (StackTraceElement e1 : es) {
            String log = "\n" + e1;
//            String log = "\n" + e1.getClassName() + "." + e1.getMethodName() + "("
//                    + e1.getFileName() + "@" + e1.getLineNumber() + ")";
            sb.append(log);
//            stackTrace.add(log);
        }
        try {
//            FileOutputStream fos = new FileOutputStream(f2);
//            byte[] b = new byte[1024];
//            b = log.getBytes();
//            fos.write(b);
//            fos.flush();
//            fos.close();
            FileWriter fw = new FileWriter(path, true);
            fw.write(sb + "########## \n");
            fw.close();
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

    }

    /**
     * 概述：<br/>
     * 适用条件：<br/>
     * 执行流程：<br/>
     * 使用方法：<br/>
     * 注意事项：<br/>
     *
     * @param args void
     * @date 2014-7-18
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}
